import {defineStore} from 'pinia';

const useStore = defineStore("main",{
    state: () => ({
        // 定义状态
        userInfo: {
            name: 'yyw1',
            age: 10,
            email: 'yyw1@163.com'
        },
        isAuthenticated: false
    }),
    getters: {
        // 定义计算属性
        userName: (state) => state.userInfo.name,
        isAdult: (state) => state.userInfo.age >= 18
    },
    actions: {
        // 定义方法
        setUserInfo(name, age, email) {
            this.userInfo.name = name;
            this.userInfo.age = age;
            this.userInfo.email = email;
        },
        authenticate() {
            this.isAuthenticated = true;
        },
        logout() {
            this.isAuthenticated = false;
            this.userInfo = { name: '', age: 0, email: '' };
        },
        printUserInfo() {
            if(this.hasOwnProperty('userInfo')){
                console.log('User Info:', this.userInfo);
            }else{
                console.error('User info is not defined');
            }
        },
        changeUserInfo(userInfo) {
            console.log('origin userInfo:', this.userInfo);
            this.userInfo = userInfo;
            console.log('updated userInfo:', this.userInfo);
        }
    }
})
export default useStore;